package com.ruoyi.standingbook.park.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.asset.homepage.constant.StatusConstant;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.standingbook.park.domain.bo.ParkPicBo;
import com.ruoyi.standingbook.park.domain.vo.ParkFinishedSumVo;
import com.ruoyi.standingbook.park.domain.vo.ParkImportDetailVo;
import com.ruoyi.standingbook.park.domain.vo.ParkPicVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.standingbook.park.domain.bo.ParkBo;
import com.ruoyi.standingbook.park.domain.vo.ParkVo;
import com.ruoyi.standingbook.park.domain.Park;
import com.ruoyi.standingbook.park.mapper.ParkMapper;
import com.ruoyi.standingbook.park.service.IParkService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 公园管养任务完成进度Service业务层处理
 *
 * @author cc
 * @date 2023-08-29
 */
@RequiredArgsConstructor
@Service
public class ParkServiceImpl implements IParkService {

    private final ParkMapper baseMapper;

    /**
     * 查询公园管养任务完成进度
     */
    @Override
    public ParkVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公园管养任务完成进度列表
     */
    @Override
    public TableDataInfo<ParkVo> queryPageList(ParkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Park> lqw = buildQueryWrapper(bo);
        Page<ParkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公园管养任务完成进度列表
     */
    @Override
    public List<ParkVo> queryList(ParkBo bo) {
        LambdaQueryWrapper<Park> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Park> buildQueryWrapper(ParkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Park> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getItemName()), Park::getItemName, bo.getItemName());
        lqw.eq(bo.getServiceType()!=null, Park::getServiceType, bo.getServiceType());
        lqw.like(StringUtils.isNotBlank(bo.getParkName()), Park::getParkName, bo.getParkName());
        lqw.eq(StringUtils.isNotBlank(bo.getCommunity()), Park::getCommunity, bo.getCommunity());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), Park::getLocation, bo.getLocation());
        lqw.eq(bo.getGreenArea() != null, Park::getGreenArea, bo.getGreenArea());
        lqw.eq(bo.getWoodlandArea() != null, Park::getWoodlandArea, bo.getWoodlandArea());
        lqw.eq(bo.getWatershedArea() != null, Park::getWatershedArea, bo.getWatershedArea());
        lqw.eq(StringUtils.isNotBlank(bo.getGreenLevel()), Park::getGreenLevel, bo.getGreenLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getWoodlandLevel()), Park::getWoodlandLevel, bo.getWoodlandLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getWatershedLevel()), Park::getWatershedLevel, bo.getWatershedLevel());
        lqw.eq(bo.getDate() != null, Park::getDate, bo.getDate());
        lqw.eq(bo.getFrequency() != null, Park::getFrequency, bo.getFrequency());
        lqw.eq(bo.getActualFrequency() != null, Park::getActualFrequency, bo.getActualFrequency());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Park::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getPic1()), Park::getPic1, bo.getPic1());
        lqw.eq(StringUtils.isNotBlank(bo.getPic2()), Park::getPic2, bo.getPic2());
        lqw.eq(StringUtils.isNotBlank(bo.getPic3()), Park::getPic3, bo.getPic3());
        lqw.eq(StringUtils.isNotBlank(bo.getPic4()), Park::getPic4, bo.getPic4());
        lqw.eq(StringUtils.isNotBlank(bo.getProviderId()), Park::getProviderId, bo.getProviderId());

        lqw.between(params.get("beginDate") != null && params.get("endDate") != null,
        		Park::getDate ,params.get("beginDate"), params.get("endDate"));

        return lqw;
    }

    /**
     * 新增公园管养任务完成进度
     */
    @Override
    public Boolean insertByBo(ParkBo bo) {
        Park add = BeanUtil.toBean(bo, Park.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公园管养任务完成进度
     */
    @Override
    public Boolean updateByBo(ParkBo bo) {
        Park update = BeanUtil.toBean(bo, Park.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Park entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公园管养任务完成进度
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean batchInsert(List<ParkImportDetailVo> voList) {
        if (null != voList && !voList.isEmpty()) {
            Date date = voList.get(0).getDate();
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            LambdaQueryWrapper<Park> lqw = Wrappers.lambdaQuery();
            lqw.eq(Park::getDate, date);
            this.baseMapper.delete(lqw);
        }

        List<Park> list = BeanCopyUtils.copyList(voList, Park.class);
        list.forEach(e -> {
            e.setId(null);
            e.restBase();
        });
        return this.baseMapper.insertBatch(list);
    }

    @Override
    public List<ParkPicVo> getParkPicByDate(String date) {
        List<ParkPicBo> parkPicBos = baseMapper.selectPicByDate(date);
        List<ParkPicVo> pics = new ArrayList<>();
        parkPicBos.forEach(e->{
            if (StringUtils.isNotBlank(e.getPic1())){
                ParkPicVo parkPicVo = new ParkPicVo();
                parkPicVo.setPic(e.getPic1());
                pics.add(parkPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic2())){
                ParkPicVo parkPicVo = new ParkPicVo();
                parkPicVo.setPic(e.getPic2());
                pics.add(parkPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic3())){
                ParkPicVo parkPicVo = new ParkPicVo();
                parkPicVo.setPic(e.getPic3());
                pics.add(parkPicVo);
            }
            if (StringUtils.isNotBlank(e.getPic4())){
                ParkPicVo parkPicVo = new ParkPicVo();
                parkPicVo.setPic(e.getPic4());
                pics.add(parkPicVo);
            }
        });
        return pics;
    }

    @Override
    public BigDecimal getAreaSumByDate(String date) {
        ParkFinishedSumVo parkFinishedSumVo = baseMapper.selectSumByDate(date, null);
        return getAreaSum(parkFinishedSumVo);
    }

    @Override
    public BigDecimal getAreaSumByDateAndCommunity(String date, String community) {
        ParkFinishedSumVo parkFinishedSumVo = baseMapper.selectSumByDate(date,community);
        return getAreaSum(parkFinishedSumVo);
    }

    private BigDecimal getAreaSum(ParkFinishedSumVo parkFinishedSumVo) {
        if (null != parkFinishedSumVo){
            BigDecimal greenAreaSum = parkFinishedSumVo.getGreenArea();
            BigDecimal watershedAreaSum = parkFinishedSumVo.getWatershedArea();
            BigDecimal woodlandAreaSum = parkFinishedSumVo.getWoodlandArea();
            return greenAreaSum.add(watershedAreaSum).add(woodlandAreaSum);
        }
        return BigDecimal.ZERO;
    }

    @Override
    public Integer getNumSumByDate(String date) {
        return baseMapper.selectNumSumByDate(date,null , StatusConstant.FINISHED);
    }

    @Override
    public List<ParkVo> queryListByDate(String date, String community) {
        LambdaQueryWrapper<Park> lqw = Wrappers.lambdaQuery();
        lqw.eq(Park::getDate,date);
        if (StringUtils.isNotEmpty(community)){
            lqw.eq(Park::getCommunity,community);
        }
        lqw.orderByDesc(Park::getGreenArea);
        return baseMapper.selectVoList(lqw);
    }
}
